


*For Margaret dropbox folders 
global NCREIF "/Users/BeckaBrolinson/Dropbox/NCREIF/data" 
global build 	"$NCREIF/build" 
global analysis "$NCREIF/analysis"
global results 	"$analysis/results"
global figures 	"$analysis/figures" 

*------------------------------------------------------------------------------*
*	Step 10- Rennovation Data	   *
*------------------------------------------------------------------------------*	


	
*Testing renovation data 

use "$build/FakeNCREIFAnnualizedDatawcontrol_postmatch2.dta", clear

	gen sqft1000s = sqft / 1000 
	gen HDD1000s = HDD / 1000 
	gen CDD1000s = CDD / 1000 
	gen used_space1000s = used_space/ 1000
	gen real_capex_ti_sqft = real_capex_ti / sqft 
	gen real_capex_bldimp_sqft = real_capex_bldimp / sqft 
	
*ssc install carryforward
*Carry forward renovation year data 
	sort propnum year
	by propnum: carryforward lastrenovatedyear, replace 

	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.
	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.
	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.
	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.
	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.
	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.
	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.
	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.
	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.
	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.
	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.
	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.
	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.
	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.
	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.
	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.

	*Test to make sure last ren year have all been filled in 
	capture drop id_unique 
	preserve 
	bysort propnum lastrenovatedyear: gen id_unique=_n==1 
	keep if id_unique==1 
	drop id_unique 
	bysort propnum: gen id_unique=_n==1 
	restore 
	*Some lastren year are listed as 0, replace these observations as missing 
	replace lastrenovatedyear=. if lastrenovatedyear==0 
	*Some of the renovated yeras are the same as year built, rpelace these as missing 
	gen cleaned_renyear= lastrenovatedyear 
	replace cleaned_renyear= . if lastrenovatedyear==yrbuilt
	*Generate a new version of year built or last ren that takes in ren year 
	gen mostrecentupdate= yrbuilt
	replace mostrecentupdate= lastrenovatedyear if lastrenovatedyear>yrbuilt & !missing(lastrenovatedyear) 
	*Generate an indicator if building is renovated between 2000-2015 
	generate d_reno = (cleaned_renyear>=2000) if !missing(cleaned_renyear)
	replace d_reno = 0 if missing(d_reno) 

	*Generate an indicator if building is built between 2000-2015 
	generate d_built = (yrbuilt>=2000) if !missing(yrbuilt)
	replace d_built=0 if missing(d_built) 
	*Generate an indicator if building is most recently updated (built or ren) between 2000-2015
	generate d_update= (mostrecentupdate>=2000) if !missing(mostrecentupdate) 
	replace d_update=0 if missing(d_update) 
	
*Generate an indicator for being in the three different CBECS regimes 
	*From 1999-2003, the 1995 CBECS were used; from 2004-2007 the 1999 CBECS were used, and from 2008-August 2018 the 2003 CBECS were used https://www.energystar.gov/buildings/facility-owners-managers/existing-buildings/use-portfolio-manager/update-energy-star-scores-cbecs
	*Generate an indicator for each rating time period 
	gen d_cert_2000_2003 = (firstyearrated >=2000 & firstyearrated<=2003)
	gen d_cert_2004_2007 = (firstyearrated >=2004 & firstyearrated<=2007)
	gen d_cert_2008_2015 = (firstyearrated >=2008 & firstyearrated<=2015)
	
*Plot building energy star scores over time 
	* a Building's score remains the *same* between certifications 
	gen rating_carryforward = rating 
	by propnum: carryforward rating_carryforward, replace 
	
	*Generate average enrgy star score within year 
	preserve 
	keep if treat==1 
	collapse (mean) rating rating_carryforward (sd) rating_sd = rating rating_carryforward_sd = rating_carryforward  , by(year)
	
	gen lower_carryforward = rating_carryforward - rating_carryforward_sd
	gen upper_carryforward = rating_carryforward + rating_carryforward_sd
	gen lower = rating - rating_sd
	gen upper = rating + rating_sd
	
	twoway 	(line rating year, /*
	*/ 				xline(2003.5, lcolor(gs10) lpattern(dash)) /* 
	*/ 		xline(2006.5, lcolor(gs10) lpattern(dash)) /* 
	*/ 		bgcolor(white) graphregion(color(white)) ylab(80(2)88, nogrid) /*
	*/ 		xlabel(2000(2)2014, angle(30))	/* 
	*/ 		ytitle("Energy Star Score") /* 
	*/ 		xtitle("Year") /* 
	*/ 	legend(pos(2) ring(0) order(1 "Avg. Annual Rating") ))
	gr export "$figures/20_averagescore.png", replace
	

	twoway 	(line rating_carryforward year, /*
	*/ 		xline(2003.5, lcolor(gs10) lpattern(dash)) /* 
	*/ 		xline(2006.5, lcolor(gs10) lpattern(dash)) /* 
	*/ 		bgcolor(white) graphregion(color(white)) ylab(80(2)88, nogrid) /*
	*/ 		xlabel(2000(2)2014, angle(30))	/* 
	*/ 	legend(pos(2) ring(0) order(1 "Avg. Annual Rating") ))
	restore 
*repeat sales regression 

	*Linear probability model for probability of being post-cert on being updated 
	set more off 

	#d ;
	global Cov0 	""; 
	global Cov1 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s 
	age2 used_space1000s
	 percentleased real_capex_ti_sqft real_capex_bldimp_sqft "; 
	global Cov2 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s 
	 age2 percentleased used_space1000s
	dFundType1 dFundType2 dFundType4 dFundType5 dFundType6";
	global Cov3	"Covered_E real_elecprice Unemployment HDD1000s CDD1000s 
	age2 used_space1000s"; 
	#d cr 

	*Generate one dummy varaible for each year 
	 tabulate year, generate(year)
	 drop if year == 2015
	 *generate log 
	 gen ln_rating = ln(rating)
	*Genreate count of buildings that get certified more than once 
	bysort propnum: gen d_cert = 1 if !missing(ind_yr)
	by propnum: egen num_cert = total(d_cert)
	codebook propnum if num_cert>=2 

*Split sample into certification right before versus right after a change in certification regimes 
	gen d_beforecbecschange = 1 if firstyearrated == 2003 | firstyearrated == 2006 
	replace d_beforecbecschange = 0 if missing(d_beforecbecschange)
	gen d_aftercbecschange = 1 if firstyearrated == 2004 | firstyearrated == 2007 
	replace d_aftercbecschange = 0 if missing(d_aftercbecschange)
	*generate interaction term to estimate different treatment effects 
	*Generate the heterogenous treatment effects interaction 
	gen interaction_beforecbecs = treat*post*d_beforecbecschange
	gen interaction_aftercbecs = treat*post*d_aftercbecschange
	
	*generate the dummy for all other buildings 
	gen d_allotherbuildings = 1 if treat == 1 & firstyearrated != 2004 & firstyearrated!= 2007 
	replace d_allotherbuildings=0  if missing(d_allotherbuildings)
	gen interaction_allother= treat*post*d_allotherbuildings
	
	**Try with just buildings that get certified in the year after cert 
	*keep if treat == 0 | firstyearrated == 2004  | firstyearrated== 2007 
	
	eststo UtilFECov1_het: reghdfe logrealutilpersf interaction_allother interaction_aftercbecs  rl_yr_rentpersf $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  UtilFECov1_het, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	eststo UtilFECov1_het1: reghdfe logrealutilpersf interaction interaction_beforecbecs interaction_aftercbecs  rl_yr_rentpersf $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  UtilFECov1_het1, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	eststo RentFECov1_het: reghdfe logrealrentpersf interaction_allother interaction_aftercbecs $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  RentFECov1_het, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	eststo RentFECov1_het1: reghdfe logrealrentpersf interaction interaction_beforecbecs interaction_aftercbecs $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  RentFECov1_het1, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	
	label var interaction_allother "Cert*Post*AllYearsButUpdate"
	label var interaction "Cert*Post" 
	label var interaction_beforecbecs "1(Cert*Post*\$<\$CBECS-Change)"
	label var interaction_aftercbecs "1(Cert*Post*\$>=\$CBECS-Change)"
	label var treat "1[treat=1]"
	label var logrealrentpersf "ln(Rent/Sq. Ft.) (\\$)"
	label var Covered_E "Benchmarking Law" 
	label var real_elecprice "Avg. Elec. Price (\\$/MWh)" 
	label var real_gasprice "Avg. Gas Price (\\$/Mcf)" 
	label var Unemployment "Unemployment" 
	label var age "Building Age" 
	label var age2 "Building Age Squared" 
	label var used_space "Used Space (Pct. Leased * Sq. Ft.)"
	label var used_space1000s "Used Space (Pct. Leased * Sq. Ft.) (1000s)"
	labe var percentleased "Percent Leased (\%)"
	label var real_capex_ti "Cap Exp. (Tenant Imp.)" 
	label var real_capex_ti_sqft "Cap Exp./Sq. Ft. (Tenant Imp.) (\\$)"
	label var real_capex_bldimp "Cap Exp. (Bldg. Imp.)"
	label var real_capex_bldimp_sqft "Cap Exp./Sq. Ft. (Bldg. Imp.) (\\$)"
	label var HDD "HDD" 
	label var HDD1000s "HDD (1000s)" 
	label var CDD "CDD" 
	label var CDD1000s "CDD (1000s)" 
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var logrealutilpersf "ln(Util/Sq. Ft.) (\\$)"
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var rl_yr_utilpersf "Util. per Sq. Ft."
	label var yrbuilt "Year Built" 
	label var sqft "Square Feet"
	label var sqft1000s "Square Feet (1000s)" 
	
	*make Latex Table 
	esttab RentFECov1_het RentFECov1_het1 UtilFECov1_het UtilFECov1_het1 using "$results/17_HetTEbyCBECSregime.tex" , label replace booktabs ///
	alignment(SSSS) ///
	b(%12.3f) se(%12.3f) star(* 0.05) /// sets format of parameters, standard errors, and stars
	stats(N, fmt(%9.0fc)) /// adds comma to Observation number
	nonotes nogaps /// removes notes from bottom of table 
	indicate(`r(indicate_fe)', label("Y" "")) /// adds y & N for FE inclusions
	title(Matched Sample: Heterogeneous Treatment Effects by CBECS Data Update \label{17HetTEbyCBECSregime}) ///
	addnote({\scriptsize * p$<$0.05. The standard errors reported in parenthesis have been clustered at the CBSA level.})
	

	
	
	 preserve 
	 eststo MatchedscoreEventStudyCov1: parmby "reghdfe rating year2-year13 $Cov1 rl_yr_rentpersf , absorb(i.propnum i.city_cat) vce(cluster cbsa) ", label norestore
	 *findit eclplot 
	*Plot the point estimates 
	keep if parmseq>=1 & parmseq<=12
	*Add row to plot the 0 point estimate 
	set obs `=_N+1'
	replace label = "2000" if label == "" 
	replace parmseq =0.5 if parmseq == . 
	replace estimate = 0 if estimate == . 
	sort parmseq

	*Change the _ to - in the label variable 
	replace label=subinstr(label, "year==  ", "", 1 )
	replace label=subinstr(label, ".0000", "", 1 )
	sencode label if parm!="_cons", gene(parmlab)

	*Plot the outcomes 
	eclplot estimate min95 max95 parmlab, ytitle("Coefficient Estimates") ///
	xtitle("Year of Certification") yline(0, lcolor(navy*.5)) ///
	xlabel(#14) bgcolor(white) graphregion(color(white)) ylab(,nogrid) legend(on lab(1 "95% CI")) 
	gr export "$figures/20_score_eventstudy_matched_repeatcert_cov1.png", replace	
	restore
	
	
	
			 preserve 
	 eststo MatchedscoreEventStudyCov1: parmby "reghdfe rating $Cov1 rl_yr_rentpersf year2-year13, absorb(i.city_cat) vce(cluster cbsa) ", label norestore
	 *findit eclplot 
	*Plot the point estimates 
	keep if parmseq>=14 & parmseq<=25
	*Add row to plot the 0 point estimate 
	set obs `=_N+1'
	replace label = "2000" if label == "" 
	replace parmseq = 13 if parmseq == . 
	replace estimate = 0 if estimate == . 
	sort parmseq

	*Change the _ to - in the label variable 
	replace label=subinstr(label, "year==  ", "", 1 )
	replace label=subinstr(label, ".0000", "", 1 )
	sencode label if parm!="_cons", gene(parmlab)

	*Plot the outcomes 
	eclplot estimate min95 max95 parmlab, ytitle("Coefficient Estimates") ///
	xtitle("Year of Certification") yline(0, lcolor(navy*.5)) ///
	xlabel(#14) bgcolor(white) graphregion(color(white)) ylab(,nogrid) legend(on lab(1 "95% CI")) 
	gr export "$figures/20_score_eventstudy_matched.png", replace	
	restore
	
	 eststo MatchedScoreEventStudyCov1: reghdfe rating year2-year13 $Cov1 rl_yr_rentpersf , absorb(i.propnum i.city_cat) vce(cluster cbsa)
	  estfe  MatchedScoreEventStudyCov1, labels(city_cat "City FE" propnum "Property FE")
	 eststo MatchedScoreEventStudy: reghdfe rating year2-year13, absorb(i.propnum i.city_cat) vce(cluster cbsa)
	 estfe  MatchedScoreEventStudy, labels(city_cat "City FE" propnum "Property FE")
	 eststo  MatchedScoreEventStudyCov3: reghdfe rating year2-year13 $Cov3 rl_yr_rentpersf , absorb(i.propnum i.city_cat) vce(cluster cbsa)
	 estfe  MatchedScoreEventStudyCov3, labels(city_cat "City FE" propnum "Property FE")
	
		*label variables for regression tables 
	label var rating "Score"
	label var interaction "Cert*Post" 
	label var treat "1[treat=1]"
	label var logrealrentpersf "ln(Rent/Sq. Ft.) (\\$)"
	label var Covered_E "Benchmarking Law" 
	label var real_elecprice "Avg. Elec. Price (\\$/MWh)" 
	label var real_gasprice "Avg. Gas Price (\\$/Mcf)" 
	label var Unemployment "Unemployment" 
	label var age "Building Age" 
	label var age2 "Building Age Squared" 
	label var used_space "Used Space (Pct. Leased * Sq. Ft.)"
	label var used_space1000s "Used Space (Pct. Leased * Sq. Ft.) (1000s)"
	labe var percentleased "Percent Leased (\%)"
	label var real_capex_ti "Cap Exp. (Tenant Imp.)" 
	label var real_capex_ti_sqft "Cap Exp./Sq. Ft. (Tenant Imp.) (\\$)"
	label var real_capex_bldimp "Cap Exp. (Bldg. Imp.)"
	label var real_capex_bldimp_sqft "Cap Exp./Sq. Ft. (Bldg. Imp.) (\\$)"
	label var HDD "HDD" 
	label var HDD1000s "HDD (1000s)" 
	label var CDD "CDD" 
	label var CDD1000s "CDD (1000s)" 
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var logrealutilpersf "ln(Util/Sq. Ft.) (\\$)"
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var rl_yr_utilpersf "Util. per Sq. Ft."
	label var yrbuilt "Year Built" 
	label var sqft "Square Feet"
	label var sqft1000s "Square Feet (1000s)" 
	label var year1 "2000"
	label var year2 "2001"
	label var year3 "2002"
	label var year4 "2003"
	label var year5 "2004"
	label var year6 "2005"
	label var year7 "2006"
	label var year8 "2007"
	label var year9 "2008"
	label var year10 "2009"
	label var year11 "2010"
	label var year12 "2011"
	label var year13 "2012"
	label var year14 "2013"
	label var year15 "2014"
	
	
	*make Latex Table 
	esttab  MatchedScoreEventStudy  MatchedScoreEventStudyCov1  MatchedScoreEventStudyCov3 using "$results/17_MatchedEventStudy_score.tex" , label replace booktabs ///
	alignment(SS) ///
	b(%12.3f) se(%12.3f) star(* 0.05) /// sets format of parameters, standard errors, and stars
	stats(N, fmt(%9.0fc)) /// adds comma to Observation number
	nonotes nogaps /// removes notes from bottom of table 
	indicate(`r(indicate_fe)', label("Y" "")) /// adds y & N for FE inclusions
	drop(year13 ///
	year12 year11) /// dropping the first and last 10 interaction terms to be able to fit table on one page
	title(Matched Sample: Energy Star Certification Event Study \label{17MatchedEventStudyscore}) ///
	addnote({\scriptsize * p$<$0.05. The standard errors reported in parenthesis have been clustered at the CBSA level.})

